﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite
{
    //该页面为用户登录后查看的首页
    public partial class IndexOn : PreBasePage
    {
        private Users uinfo = new Users();
        protected void Page_Load(object sender, EventArgs e)
        {
            uinfo = UserBase.GetCurrentUser();
            if (uinfo == null)
            {
                Response.Redirect("Login.aspx?returnurl=IndexOn.aspx");
                return;
            }
            if (!Page.IsPostBack)
            {
                BindBlog();//首页中间位置
                //BindPVTuWenOrder();//图文榜
                //BindPVVideoOrder();//视频榜
                //BindRecommandOrder();//推荐排行榜
                BindCareUserList();//我关注的创作人
                BindCompany();//我加入的小组
                BindCareCompany();//我关注的小组
                BindUserNowRead();//我的好友正在读
                //BindUserBlogNum();//才子榜
                //BindCareUser();//关注排行榜
                BindBlogPic();//图文
            }
        }

        #region 加载博客
        private void BindBlog()
        {
            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            //List<Member> list = new List<Member();
            BlogHelper bhelper = new BlogHelper();

            DataSet ds = bhelper.GetUserCareBlog(uinfo.ID, BlogUPager.Begin - 1, BlogUPager.PageSize);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                BlogUPager.ItemCount = bhelper.GetUserCareBlogCount(uinfo.ID);

                // 以下的值从BasePage中得到
                BlogUPager.UrlFormat = UrlFormat;
                //BlogUPager.PrefixText = GetPreFixText(BlogUPager);

                ds.Tables[0].Columns.Add(new DataColumn("strBlogContent"));
                ds.Tables[0].Columns.Add(new DataColumn("strHasPic"));
                ds.Tables[0].Columns.Add(new DataColumn("strVideoPic"));
                ds.Tables[0].Columns.Add(new DataColumn("companyname"));//小组

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //截取内容
                    string blogcontent = Server.HtmlDecode(dr["BlogContent"] + "");
                    //将内容中的html代码去掉
                    blogcontent = Tools.NoHTML(blogcontent);
                    string strblogcontent = Tools.GetFirstString(blogcontent, 160);
                    if (strblogcontent != "")
                    {
                        string userid = dr["UsersID"] + "";
                        //加载头像
                        UsersHelper uhelper = new UsersHelper();
                        Users ubloginfo = uhelper.GetUsers(userid);
                        string headphoto = ubloginfo.SmallHeadImage;
                        if (!string.IsNullOrEmpty(headphoto))
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"u/" + userid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"" + headphoto + "\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                        else
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"u/" + userid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"images/morentouxiang.jpg\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                    }

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    //string blogbigtype = dr["BlogBigType"] + "";
                    //if (blogbigtype == "视频")
                    //{
                    //    dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                    //}
                    //else if (blogbigtype.ToLower() == "mp3")
                    //{
                    //    dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                    //}
                    //else
                    //{
                    //    dr["strVideoPic"] = "";
                    //}

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                        dr["BlogBigType"] = "<a href=\"VideoList.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                        dr["BlogBigType"] = "<a href=\"MP3List.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                        dr["BlogBigType"] = "<a href=\"TextList.aspx\" target=\"_blank\"><font color=\"#729830\">" + blogbigtype + "</font></a>";
                    }

                    string blogtypename = dr["BlogTypeName"] + "";
                    string blogtypeid = dr["BlogTypeID"] + "";
                    dr["BlogTypeName"] = "<a href=\"cate/" + blogtypeid + ".aspx\" target=\"_blank\"><font color=\"#729830\">" + blogtypename + "</font></a>";

                    //加载小组
                    string companyid = dr["CompanyID"] + "";
                    if (!string.IsNullOrEmpty(companyid) && companyid != "0")
                    {
                        Companys cinfo = new Companys();
                        CompanysHelper chelper = new CompanysHelper();
                        cinfo = chelper.GetCompanys(companyid);
                        dr["companyname"] = "小组：" + cinfo.CompanyName;
                    }
                    else
                    {
                        dr["companyname"] = "";
                    }
                }

                rptBlog.DataSource = ds;
                rptBlog.DataBind();

                lblMessage.Visible = false;
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "关注的用户有创作和推荐将会出现在这里，<a href=\"Index.aspx\"><font color=\"red\">返回首页</font></a>";
            }
        }
        #endregion

        //#region 加载图文榜
        //private void BindPVTuWenOrder()
        //{
        //    BlogHelper bhelper = new BlogHelper();
        //    Criteria c = new Criteria();
        //    c.Add(CriteriaType.Equals, "Flag", 0);
        //    c.Add(CriteriaType.Equals, "BlogBigType", "图文");

        //    string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
        //    DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("PVCount", OrderMode.Desc) }, 0, 10, fields);

        //    DataColumn dcTitle = new DataColumn("strTitle");
        //    dtpvBlog.Columns.Add(dcTitle);
        //    dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
        //    dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

        //    if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dtpvBlog.Rows)
        //        {
        //            string title = dr["title"] + "";
        //            string strtitle = "";
        //            strtitle = Tools.GetFirstString(title, 26);
        //            dr["strTitle"] = strtitle;

        //            //判断是否存在图片
        //            string haspic = dr["haspic"] + "";
        //            if (haspic == "1")
        //            {
        //                dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
        //            }
        //            else
        //            {
        //                dr["strHasPic"] = "";
        //            }

        //            string blogbigtype = dr["BlogBigType"] + "";
        //            if (blogbigtype == "视频")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
        //            }
        //            else if (blogbigtype.ToLower() == "mp3")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
        //            }
        //            else
        //            {
        //                dr["strVideoPic"] = "";
        //            }
        //        }
        //    }
        //    rptPVTuWenOrder.DataSource = dtpvBlog;
        //    rptPVTuWenOrder.DataBind();
        //}
        //#endregion

        //#region 加载视频榜
        //private void BindPVVideoOrder()
        //{
        //    BlogHelper bhelper = new BlogHelper();
        //    Criteria c = new Criteria();
        //    c.Add(CriteriaType.Equals, "Flag", 0);
        //    c.Add(CriteriaType.Equals, "BlogBigType", "视频");

        //    string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
        //    DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("PVCount", OrderMode.Desc) }, 0, 10, fields);

        //    DataColumn dcTitle = new DataColumn("strTitle");
        //    dtpvBlog.Columns.Add(dcTitle);
        //    dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
        //    dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

        //    if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dtpvBlog.Rows)
        //        {
        //            string title = dr["title"] + "";
        //            string strtitle = "";
        //            strtitle = Tools.GetFirstString(title, 24);
        //            dr["strTitle"] = strtitle;

        //            //判断是否存在图片
        //            string haspic = dr["haspic"] + "";
        //            if (haspic == "1")
        //            {
        //                dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
        //            }
        //            else
        //            {
        //                dr["strHasPic"] = "";
        //            }

        //            string blogbigtype = dr["BlogBigType"] + "";
        //            if (blogbigtype == "视频")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
        //            }
        //            else if (blogbigtype.ToLower() == "mp3")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
        //            }
        //            else
        //            {
        //                dr["strVideoPic"] = "";
        //            }
        //        }
        //    }
        //    rptPVVideoOrder.DataSource = dtpvBlog;
        //    rptPVVideoOrder.DataBind();
        //}
        //#endregion

        //#region 加载推荐排行榜
        //private void BindRecommandOrder()
        //{
        //    BlogHelper bhelper = new BlogHelper();
        //    Criteria c = new Criteria();
        //    c.Add(CriteriaType.Equals, "Flag", 0);

        //    string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
        //    DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("RecommandCount", OrderMode.Desc) }, 0, 10, fields);

        //    DataColumn dcTitle = new DataColumn("strTitle");
        //    dtpvBlog.Columns.Add(dcTitle);
        //    dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
        //    dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

        //    if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dtpvBlog.Rows)
        //        {
        //            string title = dr["title"] + "";
        //            string strtitle = "";
        //            strtitle = Tools.GetFirstString(title, 26);
        //            dr["strTitle"] = strtitle;

        //            //判断是否存在图片
        //            string haspic = dr["haspic"] + "";
        //            if (haspic == "1")
        //            {
        //                dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
        //            }
        //            else
        //            {
        //                dr["strHasPic"] = "";
        //            }

        //            string blogbigtype = dr["BlogBigType"] + "";
        //            if (blogbigtype == "视频")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
        //            }
        //            else if (blogbigtype.ToLower() == "mp3")
        //            {
        //                dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
        //            }
        //            else
        //            {
        //                dr["strVideoPic"] = "";
        //            }
        //        }
        //    }

        //    rptRecommandOrder.DataSource = dtpvBlog;
        //    rptRecommandOrder.DataBind();
        //}
        //#endregion

        #region 加载精彩图文
        private void BindBlogPic()
        {
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "HasPic", 1);
            string systemuserid = System.Configuration.ConfigurationManager.AppSettings["systemaduserid"] + "";
            c.Add(CriteriaType.NotEquals, "UsersID", systemuserid);

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "FirstPicPath", "Flag", "CreateDate" };
            DataTable dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 6, fields);
            dt.Columns.Add(new DataColumn("strTitle"));

            foreach (DataRow dr in dt.Rows)
            {
                string title = dr["Title"] + "";
                title = Tools.GetFirstString(title, 30);
                dr["strTitle"] = title;
            }

            rptBlogPic.DataSource = dt;
            rptBlogPic.DataBind();
        }
        #endregion

        #region 加载我关注的用户列表
        private void BindCareUserList()
        {
            Users uinfo = UserBase.GetCurrentUser();
            FriendHelper fhelper = new FriendHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);

            DataTable dt = fhelper.QueryFriendBySearchToTable(c, null, 0, 10, null);
            
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(new DataColumn("blognum"));
                foreach (DataRow dr in dt.Rows)
                {
                    string friendid = dr["FriendUsersID"] + "";
                    //加载该用户的博客数
                    Users friendinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    friendinfo = uhelper.GetUsers(friendid);
                    string blognum = friendinfo.BlogNum.ToString();
                    dr["blognum"] = blognum;
                }
            }
            rptWeCareUser.DataSource = dt;
            rptWeCareUser.DataBind();
        }
        #endregion

        #region 加载好友正在阅读
        private void BindUserNowRead()
        {
            BlogHelper bhelper = new BlogHelper();
            DataSet dtpvBlog = bhelper.GetFriendNowRead(uinfo.ID, 10);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtpvBlog.Tables[0].Columns.Add(dcTitle);
            dtpvBlog.Tables[0].Columns.Add(new DataColumn("strHasPic"));
            dtpvBlog.Tables[0].Columns.Add(new DataColumn("strVideoPic"));

            if (dtpvBlog != null && dtpvBlog.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dtpvBlog.Tables[0].Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 24);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }
            rptFriendNowRead.DataSource = dtpvBlog;
            rptFriendNowRead.DataBind();
        }
        #endregion

        #region 加载我加入的小组
        private void BindCompany()
        {
            UserCompanyHelper uchelper = new UserCompanyHelper();
            DataSet dsCompany = uchelper.GetUserAddCompany(uinfo.ID,5);

            
            if (dsCompany != null && dsCompany.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsCompany.Tables[0].Rows)
                {
                    string smallheadimage = dr["SmallHeadImage"] + "";
                    if (string.IsNullOrEmpty(smallheadimage))
                    {
                        dr["SmallHeadImage"] = "images/xiaozuphoto.jpg";
                    }
                }
            }
            dlCompanyList.DataSource = dsCompany;
            dlCompanyList.DataBind();
        }
        #endregion

        #region 加载我关注的小组
        private void BindCareCompany()
        {
            UserCareHelper uchelper = new UserCareHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            c.Add(CriteriaType.Equals,"ObjectType",1);

            DataTable dt = uchelper.QueryUserCareBySearchToTable(c, null, 0, 5, null);
            dt.Columns.Add(new DataColumn("RealmName"));
            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("BlogPVNum"));
            dt.Columns.Add(new DataColumn("Brief"));
            dt.Columns.Add(new DataColumn("SmallHeadImage"));
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string objectid = dr["objectid"] + "";
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(objectid);
                    string companyname = cinfo.CompanyName;
                    dr["CompanyName"] = companyname;
                    dr["RealmName"] = cinfo.RealmName;
                    dr["BlogPVNum"] = cinfo.BlogPVNum.ToString();
                    dr["Brief"] = cinfo.Brief;
                    string smallheadimage = cinfo.SmallHeadImage;
                    if (string.IsNullOrEmpty(smallheadimage))
                    {
                        dr["SmallHeadImage"] = "images/xiaozuphoto.jpg";
                    }
                    else
                    {
                        dr["SmallHeadImage"] = smallheadimage;
                    }
                }
            }

            dlCareCompany.DataSource = dt;
            dlCareCompany.DataBind();
        }
        #endregion
    }
}